package boogui.admin;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.plaf.FontUIResource;

import boogui.member.S_UISetting;
import boogui.movie.MovieVO;

public class S_MovieRegistryDialog extends JDialog {

	private static S_MovieRegistryDialog dialog;
	private RegiActionListener al = null;
	private MovieRegistryService mrs = null;

	private JPanel imgpanel;
	private JPanel contentPane;
	private JTextField title;
	private JTextField genre1;
	private JTextField genre2;
	private JTextField director;
	private JTextField actor1;
	private JTextField actor2;
	private JTextField actor3;
	private JComboBox<String> ratingCbx;
	private JTextArea synop;
	private JTextField runningtime;
	private JComboBox<String> playYearCbx;
	private JComboBox<String> playMonthCbx;
	private JComboBox<String> playDayCbx;
	private JTextField filename;
	private JButton opendirBtn;
	private JLabel imglbl;

	/*public static void main(String[] args) {
		try {
			dialog = new S_MovieRegistryDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/

	public S_MovieRegistryDialog() {	
		setModal(true);
		al = new RegiActionListener();
		mrs = new MovieRegistryService();
		setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/menu/movie.png")));
		S_UISetting.setUIFont(new FontUIResource("굴림", Font.PLAIN, 12));
		setTitle("영화 등록");
		setBounds(100, 100, 500, 555);
		setResizable(false);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JPanel basicInfoP = new JPanel();
		contentPane.add(basicInfoP, BorderLayout.CENTER);
		GridBagLayout gbl_basicInfoP = new GridBagLayout();
		gbl_basicInfoP.columnWidths = new int[] { 15, 0, 5, 0, 0, 0, 0, 0 };
		gbl_basicInfoP.rowHeights = new int[] { 20, 0, 5, 0, 5, 0, 5, 0, 5, 0,
				5, 0, 5, 0, 0 };
		gbl_basicInfoP.columnWeights = new double[] { 0.0, 0.0, 0.0, 1.0, 1.0,
				1.0, 1.0, Double.MIN_VALUE };
		gbl_basicInfoP.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		basicInfoP.setLayout(gbl_basicInfoP);

		JLabel lblNewLabel = new JLabel("제       목");
		lblNewLabel.setFont(new Font("맑은 고딕", Font.BOLD, 13));
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.gridx = 1;
		gbc_lblNewLabel.gridy = 1;
		basicInfoP.add(lblNewLabel, gbc_lblNewLabel);

		title = new JTextField();
		title.setMargin(new Insets(3, 3, 3, 3));
		GridBagConstraints gbc_title = new GridBagConstraints();
		gbc_title.gridwidth = 3;
		gbc_title.insets = new Insets(0, 0, 5, 5);
		gbc_title.fill = GridBagConstraints.HORIZONTAL;
		gbc_title.gridx = 3;
		gbc_title.gridy = 1;
		basicInfoP.add(title, gbc_title);
		title.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("장       르");
		lblNewLabel_1.setFont(new Font("맑은 고딕", Font.BOLD, 13));
		GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
		gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_1.gridx = 1;
		gbc_lblNewLabel_1.gridy = 3;
		basicInfoP.add(lblNewLabel_1, gbc_lblNewLabel_1);

		genre1 = new JTextField();
		genre1.setMargin(new Insets(3, 3, 3, 3));
		GridBagConstraints gbc_genre1 = new GridBagConstraints();
		gbc_genre1.insets = new Insets(0, 0, 5, 5);
		gbc_genre1.fill = GridBagConstraints.HORIZONTAL;
		gbc_genre1.gridx = 3;
		gbc_genre1.gridy = 3;
		basicInfoP.add(genre1, gbc_genre1);
		genre1.setColumns(10);
		
		genre2 = new JTextField();
		genre2.setMargin(new Insets(3, 3, 3, 3));
		GridBagConstraints gbc_genre2 = new GridBagConstraints();
		gbc_genre2.insets = new Insets(0, 0, 5, 5);
		gbc_genre2.fill = GridBagConstraints.HORIZONTAL;
		gbc_genre2.gridx = 4;
		gbc_genre2.gridy = 3;
		basicInfoP.add(genre2, gbc_genre2);
		genre2.setColumns(10);

		JLabel lblNewLabel_2 = new JLabel("감       독");
		lblNewLabel_2.setFont(new Font("맑은 고딕", Font.BOLD, 13));
		GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
		gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_2.gridx = 1;
		gbc_lblNewLabel_2.gridy = 5;
		basicInfoP.add(lblNewLabel_2, gbc_lblNewLabel_2);

		director = new JTextField();
		director.setMargin(new Insets(3, 3, 3, 3));
		GridBagConstraints gbc_director = new GridBagConstraints();
		gbc_director.gridwidth = 3;
		gbc_director.insets = new Insets(0, 0, 5, 5);
		gbc_director.fill = GridBagConstraints.HORIZONTAL;
		gbc_director.gridx = 3;
		gbc_director.gridy = 5;
		basicInfoP.add(director, gbc_director);
		director.setColumns(10);

		JLabel lblNewLabel_3 = new JLabel("배       우");
		lblNewLabel_3.setFont(new Font("맑은 고딕", Font.BOLD, 13));
		GridBagConstraints gbc_lblNewLabel_3 = new GridBagConstraints();
		gbc_lblNewLabel_3.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_3.gridx = 1;
		gbc_lblNewLabel_3.gridy = 7;
		basicInfoP.add(lblNewLabel_3, gbc_lblNewLabel_3);

		actor1 = new JTextField();
		actor1.setMargin(new Insets(3, 3, 3, 3));
		GridBagConstraints gbc_actor1 = new GridBagConstraints();
		gbc_actor1.insets = new Insets(0, 0, 5, 5);
		gbc_actor1.fill = GridBagConstraints.HORIZONTAL;
		gbc_actor1.gridx = 3;
		gbc_actor1.gridy = 7;
		basicInfoP.add(actor1, gbc_actor1);
		actor1.setColumns(10);

		actor2 = new JTextField();
		actor2.setMargin(new Insets(3, 3, 3, 3));
		GridBagConstraints gbc_actor2 = new GridBagConstraints();
		gbc_actor2.insets = new Insets(0, 0, 5, 5);
		gbc_actor2.fill = GridBagConstraints.HORIZONTAL;
		gbc_actor2.gridx = 4;
		gbc_actor2.gridy = 7;
		basicInfoP.add(actor2, gbc_actor2);
		actor2.setColumns(10);

		actor3 = new JTextField();
		actor3.setMargin(new Insets(3, 3, 3, 3));
		GridBagConstraints gbc_actor3 = new GridBagConstraints();
		gbc_actor3.insets = new Insets(0, 0, 5, 5);
		gbc_actor3.fill = GridBagConstraints.HORIZONTAL;
		gbc_actor3.gridx = 5;
		gbc_actor3.gridy = 7;
		basicInfoP.add(actor3, gbc_actor3);
		actor3.setColumns(10);

		JLabel lblNewLabel_4 = new JLabel("상영 시간");
		lblNewLabel_4.setFont(new Font("맑은 고딕", Font.BOLD, 13));
		GridBagConstraints gbc_lblNewLabel_4 = new GridBagConstraints();
		gbc_lblNewLabel_4.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_4.gridx = 1;
		gbc_lblNewLabel_4.gridy = 9;
		basicInfoP.add(lblNewLabel_4, gbc_lblNewLabel_4);

		runningtime = new JTextField();
		runningtime.setMargin(new Insets(3, 3, 3, 3));
		GridBagConstraints gbc_runningtime = new GridBagConstraints();
		gbc_runningtime.fill = GridBagConstraints.HORIZONTAL;
		gbc_runningtime.gridwidth = 2;
		gbc_runningtime.insets = new Insets(0, 0, 5, 5);
		gbc_runningtime.gridx = 3;
		gbc_runningtime.gridy = 9;
		basicInfoP.add(runningtime, gbc_runningtime);
		runningtime.setColumns(10);

		JLabel lblNewLabel_5 = new JLabel("분");
		GridBagConstraints gbc_lblNewLabel_5 = new GridBagConstraints();
		gbc_lblNewLabel_5.anchor = GridBagConstraints.WEST;
		gbc_lblNewLabel_5.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_5.gridx = 5;
		gbc_lblNewLabel_5.gridy = 9;
		basicInfoP.add(lblNewLabel_5, gbc_lblNewLabel_5);

		JLabel lblNewLabel_6 = new JLabel("등       급");
		lblNewLabel_6.setFont(new Font("맑은 고딕", Font.BOLD, 13));
		GridBagConstraints gbc_lblNewLabel_6 = new GridBagConstraints();
		gbc_lblNewLabel_6.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_6.gridx = 1;
		gbc_lblNewLabel_6.gridy = 11;
		basicInfoP.add(lblNewLabel_6, gbc_lblNewLabel_6);

		String[] rating = { "전체 관람가", "12세 관람가", "15세 관람가", "청소년 관람불가" };
		ratingCbx = new JComboBox(rating);
		GridBagConstraints gbc_ratingCbx = new GridBagConstraints();
		gbc_ratingCbx.gridwidth = 3;
		gbc_ratingCbx.insets = new Insets(0, 0, 5, 5);
		gbc_ratingCbx.fill = GridBagConstraints.HORIZONTAL;
		gbc_ratingCbx.gridx = 3;
		gbc_ratingCbx.gridy = 11;
		basicInfoP.add(ratingCbx, gbc_ratingCbx);

		JLabel lblNewLabel_7 = new JLabel("개  봉  일");
		lblNewLabel_7.setFont(new Font("맑은 고딕", Font.BOLD, 13));
		GridBagConstraints gbc_lblNewLabel_7 = new GridBagConstraints();
		gbc_lblNewLabel_7.insets = new Insets(0, 0, 0, 5);
		gbc_lblNewLabel_7.gridx = 1;
		gbc_lblNewLabel_7.gridy = 13;
		basicInfoP.add(lblNewLabel_7, gbc_lblNewLabel_7);

		String[] year = new String[2];
		for (int i = 0; i < year.length; ++i) {
			year[i] = (2013 + i) + "년";
		}
		playYearCbx = new JComboBox(year);
		GridBagConstraints gbc_playYearCbx = new GridBagConstraints();
		gbc_playYearCbx.insets = new Insets(0, 0, 0, 5);
		gbc_playYearCbx.fill = GridBagConstraints.HORIZONTAL;
		gbc_playYearCbx.gridx = 3;
		gbc_playYearCbx.gridy = 13;
		basicInfoP.add(playYearCbx, gbc_playYearCbx);

		String[] month = new String[12];
		for (int i = 0; i < 9; ++i) {
			month[i] = "0" + (i + 1) + "월";
		}
		for (int i = 9; i < month.length; ++i) {
			month[i] = (i + 1) + "월";
		}
		playMonthCbx = new JComboBox(month);
		GridBagConstraints gbc_playMonthCbx = new GridBagConstraints();
		gbc_playMonthCbx.insets = new Insets(0, 0, 0, 5);
		gbc_playMonthCbx.fill = GridBagConstraints.HORIZONTAL;
		gbc_playMonthCbx.gridx = 4;
		gbc_playMonthCbx.gridy = 13;
		basicInfoP.add(playMonthCbx, gbc_playMonthCbx);

		String[] day = new String[31];
		for (int i = 0; i < 9; ++i) {
			day[i] = "0" + (i + 1) + "일";
		}
		for (int i = 9; i < day.length; ++i) {
			day[i] = (i + 1) + "일";
		}
		playDayCbx = new JComboBox(day);
		GridBagConstraints gbc_playDayCbx = new GridBagConstraints();
		gbc_playDayCbx.insets = new Insets(0, 0, 0, 5);
		gbc_playDayCbx.fill = GridBagConstraints.HORIZONTAL;
		gbc_playDayCbx.gridx = 5;
		gbc_playDayCbx.gridy = 13;
		basicInfoP.add(playDayCbx, gbc_playDayCbx);

		JPanel openFileP = new JPanel();
		openFileP.setPreferredSize(new Dimension(170, 10));
		contentPane.add(openFileP, BorderLayout.EAST);
		GridBagLayout gbl_openFileP = new GridBagLayout();
		gbl_openFileP.columnWidths = new int[] { 0, 0, 0, 0, 15, 0 };
		gbl_openFileP.rowHeights = new int[] { 15, 0, 220, 0, 0, 0 };
		gbl_openFileP.columnWeights = new double[] { 1.0, 0.0, 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		gbl_openFileP.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		openFileP.setLayout(gbl_openFileP);

		JLabel label = new JLabel("파일 이름");
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.anchor = GridBagConstraints.WEST;
		gbc_label.insets = new Insets(0, 0, 5, 5);
		gbc_label.gridx = 0;
		gbc_label.gridy = 3;
		openFileP.add(label, gbc_label);

		JLabel lblNewLabel_8 = new JLabel("포스터 이미지");
		lblNewLabel_8.setFont(new Font("맑은 고딕", Font.BOLD, 13));
		GridBagConstraints gbc_lblNewLabel_8 = new GridBagConstraints();
		gbc_lblNewLabel_8.gridwidth = 5;
		gbc_lblNewLabel_8.fill = GridBagConstraints.BOTH;
		gbc_lblNewLabel_8.insets = new Insets(0, 0, 5, 0);
		gbc_lblNewLabel_8.gridx = 0;
		gbc_lblNewLabel_8.gridy = 1;
		openFileP.add(lblNewLabel_8, gbc_lblNewLabel_8);

		imgpanel = new JPanel();
		imgpanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		GridBagConstraints gbc_imgpanel = new GridBagConstraints();
		gbc_imgpanel.gridwidth = 4;
		gbc_imgpanel.fill = GridBagConstraints.BOTH;
		gbc_imgpanel.insets = new Insets(0, 0, 5, 5);
		gbc_imgpanel.gridx = 0;
		gbc_imgpanel.gridy = 2;
		openFileP.add(imgpanel, gbc_imgpanel);
		imgpanel.setLayout(new BoxLayout(imgpanel, BoxLayout.X_AXIS));
		imglbl = new JLabel("");
		imgpanel.add(imglbl);

		opendirBtn = new JButton("...");
		opendirBtn.setMinimumSize(new Dimension(25, 23));
		opendirBtn.setPreferredSize(new Dimension(35, 23));
		opendirBtn.addActionListener(al);
		opendirBtn.setActionCommand("openfile");

		filename = new JTextField();
		filename.setMargin(new Insets(3, 3, 3, 3));
		GridBagConstraints gbc_filename = new GridBagConstraints();
		gbc_filename.gridwidth = 3;
		gbc_filename.insets = new Insets(0, 0, 0, 5);
		gbc_filename.fill = GridBagConstraints.HORIZONTAL;
		gbc_filename.gridx = 0;
		gbc_filename.gridy = 4;
		openFileP.add(filename, gbc_filename);
		filename.setColumns(10);
		GridBagConstraints gbc_opendirBtn = new GridBagConstraints();
		gbc_opendirBtn.insets = new Insets(0, 0, 0, 5);
		gbc_opendirBtn.anchor = GridBagConstraints.EAST;
		gbc_opendirBtn.gridx = 3;
		gbc_opendirBtn.gridy = 4;
		openFileP.add(opendirBtn, gbc_opendirBtn);

		JPanel buttonPane = new JPanel();
		buttonPane.setPreferredSize(new Dimension(10, 200));
		getContentPane().add(buttonPane, BorderLayout.SOUTH);
		GridBagLayout gbl_buttonPane = new GridBagLayout();
		gbl_buttonPane.columnWidths = new int[] { 15, 0, 0, 15, 15 };
		gbl_buttonPane.rowHeights = new int[] { 23, 0, 0, 0, 0 };
		gbl_buttonPane.columnWeights = new double[] { 0.0, 1.0, 0.0, 0.0,
				Double.MIN_VALUE };
		gbl_buttonPane.rowWeights = new double[] { 0.0, 1.0, 0.0, 0.0,
				Double.MIN_VALUE };
		buttonPane.setLayout(gbl_buttonPane);

		JLabel lblNewLabel_10 = new JLabel("줄 거 리");
		lblNewLabel_10.setFont(new Font("맑은 고딕", Font.BOLD, 13));
		GridBagConstraints gbc_lblNewLabel_10 = new GridBagConstraints();
		gbc_lblNewLabel_10.anchor = GridBagConstraints.WEST;
		gbc_lblNewLabel_10.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_10.gridx = 1;
		gbc_lblNewLabel_10.gridy = 0;
		buttonPane.add(lblNewLabel_10, gbc_lblNewLabel_10);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setPreferredSize(new Dimension(150, 90));
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.gridwidth = 3;
		gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 1;
		gbc_scrollPane.gridy = 1;
		buttonPane.add(scrollPane, gbc_scrollPane);

		synop = new JTextArea();
		synop.setLineWrap(true);
		scrollPane.setViewportView(synop);
		synop.setPreferredSize(new Dimension(150, 1000));

		JPanel panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.gridwidth = 3;
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 1;
		gbc_panel.gridy = 3;
		buttonPane.add(panel, gbc_panel);
		{
			JButton okButton = new JButton("등 록");
			panel.add(okButton);
			okButton.addActionListener(al);
			okButton.setActionCommand("OK");
			getRootPane().setDefaultButton(okButton);
		}
		{
			JButton cancelButton = new JButton("취 소");
			panel.add(cancelButton);
			cancelButton.addActionListener(al);
			cancelButton.setActionCommand("Cancel");
		}
		
		setVisible(true);
	}

	private class RegiActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			switch (e.getActionCommand()) {
			case "openfile":				
				mrs.showImage(imglbl, filename);				
				break;
			case "OK":
				MovieVO m = new MovieVO();
				MovieRegistryService mrs = new MovieRegistryService();
				String playdate = (String)playYearCbx.getSelectedItem()
						+ (String)playMonthCbx.getSelectedItem()
						+ (String)playDayCbx.getSelectedItem();				
				
				
				m.setMovtitle(title.getText());
				m.setMovgenre1(genre1.getText());
				m.setMovgenre2(genre2.getText());
				m.setMovdirector(director.getText());
				m.setMovactor1(actor1.getText());
				m.setMovactor2(actor2.getText());
				m.setMovactor3(actor3.getText());				
				m.setMovrating((String)ratingCbx.getSelectedItem());
				m.setMovsynop(synop.getText());
				m.setMovruntime(runningtime.getText());
				m.setMovplaydate(mrs.extractNum(playdate));
				m.setMovisplay("N");
				m.setMovresv("0.0");
				boolean b = mrs.createMovie(m, filename.getText());
				if (b) {
					setVisible(false);
					dispose();
				}
				break;
			case "Cancel":
				setVisible(false);
				dispose();
				break;
			}
		}
	}
}
